package service.impl;/*
 *徐韬
 *2021/10/9
 *11:08
 */

import Dao.impl.ProductDaoimpl;
import Dao.productDao;
import Util.DruidUtil;
import Util.Page;
import entity.Product;
import service.productService;

import java.util.List;

public class productServiceimpl implements productService {
    productDao productDao = new ProductDaoimpl();
    @Override
    public List<Product> showAll(Page page) {
        List<Product> productList =null;
        productList = productDao.selectAll(page);
        Long totalCount = productDao.selectCount();
        page.setTotalCount(totalCount);
        DruidUtil.close();
        return productList;

    }

    @Override
    public int addproduct(Product product, Integer cId) {
        int result = 0;
        try {
            DruidUtil.begin();
            result = productDao.insert(product,cId);
            DruidUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            DruidUtil.rollback();
        } finally {
            DruidUtil.close();
        }
        return result;

    }

    @Override
    public Product show(Integer pId) {
        Product product = null;
        product = productDao.select(pId);
        DruidUtil.close();
        return product;
    }

    @Override
    public int update(Product product, Integer cId) {
        int result = 0;
        try {
            DruidUtil.begin();
            result = productDao.update(product,cId);
            DruidUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            DruidUtil.rollback();
        } finally {
            DruidUtil.close();
        }
        return result;

    }

    @Override
    public int delete(Integer id) {
        int result = 0;
        try {
            DruidUtil.begin();
            result = productDao.delete(id);
            DruidUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            DruidUtil.rollback();
        } finally {
            DruidUtil.close();
        }
        return result;
    }
}
